package com.lcn.rabbitmq;

import com.lcn.pojo.netty.ChatMsg;
import com.lcn.service.ChatMessageService;
import com.lcn.utils.JsonUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMQConsumer {

    @Resource
    private ChatMessageService chatMessageService;

    /**
     * 监听聊天记录持久化消息
     *
     * @param payload
     * @param message
     */
    @RabbitListener(queues = {ChatMsgMQConfig.CHAT_MSG_QUEUE})
    public void watchMsgQueue(String payload, Message message) {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        if (routingKey.equals(ChatMsgMQConfig.CHAT_MSG_ROUTING_KEY)) {
            String msg = payload;
            ChatMsg chatMsg = JsonUtils.jsonToPojo(msg, ChatMsg.class);
            chatMessageService.saveChatMessage(chatMsg);
        }
    }

}
